import 'dart:async';
import 'dart:convert';

import 'package:http/http.dart' as http;
import 'package:sow_app_core/sow_app_core.dart';

class AjaxClient {
  Future<http.Response> invoke(
    Uri uri,
    RequestType type, {
    required Map<String, String> headers,
    Map<String, dynamic>? body,
    String? contentType,
  }) async {
    http.Response response;

    body?.removeWhere((key, value) => value == null);
    body?.forEach((key, value) => body[key] = value.toString());
    switch (type) {
      case RequestType.get:
        response = await http.get(
            uri.replace(
              queryParameters: body,
            ),
            headers: headers);
        break;
      case RequestType.post:
        response = await http.post(uri, headers: headers, body: body);
        break;
      // case RequestType.put:
      //   response =
      //       await http.put(uri, headers: headers, body: jsonEncode(body));
      //   break;
      case RequestType.delete:
        response =
            await http.delete(uri, headers: headers, body: jsonEncode(body));
        break;
    }

    return response;
  }
}
